home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / vuser / slave.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-20  |  2.6 KB  |  119 lines

  1. /* slave.c - This module contains the code to handle processing the slave
  2.    process.
  3.  
  4.     Copyright 1989 by Jeffrey F. Lawhorn  (jeffl@berick.uucp)
  5.  
  6.     This file is part of vuser.
  7.  
  8.     vuser is free software; you can redistribute it and/or modify it
  9.     under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation; either version 1, or (at your
  11.     option) any later version.
  12.  
  13.     vuser is distributed in the hope that it will be useful, but
  14.     WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.     General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU General Public License
  19.     along with GNU CC; see the file COPYING.  If not, write to the
  20.     Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22.  
  23. #if !defined(lint)
  24. static char SCCSid[] = "$Id: slave.c,v 1.1 89/12/15 21:31:13 jeffl Exp $";
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #include <signal.h>
  29. /* #include <sys/wait.h> */
  30.  
  31. #if defined(__STDC__)
  32. extern void Tty2ShellMode();
  33. extern void SlavePty2Stdio();
  34. extern char *getenv(char *);
  35. #else
  36. extern void Tty2ShellMode();
  37. extern void SlavePty2Stdio();
  38. extern char *getenv();
  39. #endif
  40.  
  41. int SlavePid = 0;
  42. char **Program2Run = (char **)NULL;
  43. char *RloginProgram[] = {
  44.     "rlogin",
  45.     "rlogin",
  46.     NULL,
  47.     "-l",
  48.     NULL,
  49.     NULL,
  50. };
  51.  
  52. void RunSlaveProgram()
  53. {
  54.     char *ptr = (char *)NULL;
  55.     char tmp[1024];
  56.  
  57.     if(RloginProgram[2]) {
  58.     execlp(RloginProgram[0], RloginProgram[1], RloginProgram[2],
  59.            RloginProgram[3], RloginProgram[4], RloginProgram[5]);
  60.     perror("rlogin: ");
  61.     Tty2ShellMode();
  62.     exit(1);
  63.     }
  64.     if(Program2Run) {
  65.     execvp(Program2Run[0], Program2Run);
  66.     perror(Program2Run[0]);
  67.     Tty2ShellMode();
  68.     exit(1);
  69.     }
  70.     if(!(ptr = getenv("SHELL")))
  71.     strcpy(tmp, "/bin/sh");
  72.     else
  73.     strcpy(tmp, ptr);
  74.     execlp(tmp, tmp, (char *)0);
  75.     perror("starting slave: ");
  76.     Tty2ShellMode();
  77.     exit(1);
  78. }
  79.  
  80. void KillSlave()
  81. {
  82.     kill(SlavePid, SIGTERM);
  83.     sleep(15);
  84.     kill(SlavePid, SIGKILL);
  85.     sleep(2);
  86.     Tty2ShellMode();
  87.     exit(1);
  88. }
  89.  
  90. DeadSlave()
  91. {
  92.     /* union wait status; */
  93.      unsigned int status;
  94.  
  95.     if(wait(&status) != SlavePid) {
  96.     signal(SIGCLD, DeadSlave);
  97.     fprintf(stderr, "Someone else's child just killed me!!!\n");
  98.     fprintf(stderr, "Shutting down.\n");
  99.     KillSlave();
  100.     }
  101.     Tty2ShellMode();
  102.     exit(0);
  103. }
  104.  
  105. void StartSlave()
  106. {
  107.     signal(SIGCLD, DeadSlave);
  108.     if((SlavePid = fork()) < 0) {
  109.     perror("on fork: ");
  110.     Tty2ShellMode();
  111.     exit(1);
  112.     }
  113.     if(!SlavePid) {
  114.     SlavePty2Stdio();
  115.     RunSlaveProgram();
  116.     }
  117.     return;
  118. }
  119.